﻿<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0 ,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <title>结算页面</title>

    <link href="../AmazeUI-2.4.2/assets/css/amazeui.css" rel="stylesheet"
          type="text/css"/>

    <link href="../basic/css/demo.css" rel="stylesheet" type="text/css"/>
    <link href="../css/cartstyle.css" rel="stylesheet" type="text/css"/>

    <link href="../css/jsstyle.css" rel="stylesheet" type="text/css"/>

    <script type="text/javascript" src="../js/address.js"></script>
    <script src="../HT/assets/layer/layer.js" type="text/javascript"></script>
</head>

<body>

<!--顶部导航条 -->
<div class="am-container header">
    <ul class="message-l">
        <div class="topMessage">
            <div class="menu-hd">
                <a target="_top" class="h">亲，请登录</a> <a
                    target="_top">免费注册</a>
            </div>
        </div>
    </ul>
    <ul class="message-r">
        <div class="topMessage home">
            <div class="menu-hd">
                <a target="_top" class="h">商城首页</a>
            </div>
        </div>
        <div class="topMessage my-shangcheng">
            <div class="menu-hd MyShangcheng">
                <a target="_top"><i class="am-icon-user am-icon-fw"></i>个人中心</a>
            </div>
        </div>
        <div class="topMessage mini-cart">
            <div class="menu-hd">
                <a id="mc-menu-hd" href="shopcart.html" target="_top"><i
                        class="am-icon-shopping-cart  am-icon-fw"></i><span>购物车</span><strong
                        id="J_MiniCartNum" class="h">0</strong></a>
            </div>
        </div>
        <div class="topMessage favorite">
            <div class="menu-hd">
                <!--                <a target="_top"><i class="am-icon-heart am-icon-fw"></i><span>收藏夹</span></a>-->
            </div>
        </div>
    </ul>
</div>

<!--悬浮搜索框-->

<div class="nav white">
    <div class="logo">
        <img src="../images/logo.png"/>
    </div>
    <div class="logoBig">
        <li><img src="../images/logobig.png"/></li>
    </div>

    <div class="search-bar pr">
        <a name="index_none_header_sysc"></a>
        <form>
            <input id="searchInput" name="index_none_header_sysc" type="text"
                   placeholder="搜索" autocomplete="off"> <input
                id="ai-topsearch" class="submit am-btn" value="搜索" index="1"
                type="button">
        </form>
    </div>
</div>
<script type="text/javascript">
    //点击搜索按钮
    $("#ai-topsearch").click(function () {
        if ($("#searchInput").val().length == 0) {
            layer.msg('未输入关键字!', {icon: 1, time: 1000});
            return;
        }
        location.href = "./search.html?key=" + $("#searchInput").val();
    });
</script>
<div class="clear"></div>
<div class="concent">
    <!--地址 -->
    <div class="paycont">
        <div class="address">
            <h3>确认收货地址</h3>
            <div class="control">
                <div class="tc-btn createAddr theme-login am-btn am-btn-danger">使用新地址</div>
            </div>
            <div class="clear"></div>
            <ul class="address-ul">
            </ul>
            <div class="clear"></div>
        </div>
        <div class="clear"></div>
        <div>
            <!--支付方式-->
            <div class="logistics">
                <h3>提供的支付方式</h3>
                <ul class="pay-list">
                    <li class="pay taobao"><img src="../images/zhifubao.jpg"/>支付宝<span></span></li>
                </ul>
            </div>
            <div class="clear"></div>
            <div class="order-div">
            </div>
        </div>

        <!--信息 -->
        <div class="order-go clearfix">
            <div class="pay-confirm clearfix">
                <div class="box">
                    <div tabindex="0" id="holyshit267" class="realPay">
                        <em class="t">需付款：</em> <span class="price g_price "> <span>¥</span>
								<em class="style-large-bold-red " id="J_ActualFee">244.00</em>
							</span>
                    </div>

                    <div id="holyshit268" class="pay-address" values="">

                        <p class="buy-footer-address">
                            <span class="buy-line-title buy-line-title-type">寄送至：</span> <span
                                class="buy--address-detail"> <span class="province address-pro">湖北</span>&nbsp;
									<span class="city address-city">武汉</span>&nbsp; <span
                                    class="dist address-dist">洪山</span>&nbsp; <span
                                    class="street address-street">雄楚大道666号(中南财经政法大学)</span>
								</span> </span>
                        </p>
                        <p class="buy-footer-address">
                            <span class="buy-line-title">收货人：</span> <span
                                class="buy-address-detail"> <span class="buy-user address-name">艾迪
								</span> <span class="buy-phone address-phone">15871145629</span>
								</span>
                        </p>
                    </div>
                </div>

                <div id="holyshit269" class="submitOrder">
                    <div class="go-btn-wrap">
                        <a id="J_Go" class="btn-go" tabindex="0"
                           title="点击此按钮，提交订单">提交订单</a>
                    </div>
                </div>
                <div class="clear"></div>
            </div>
        </div>
    </div>

    <div class="clear"></div>
</div>
</div>
<div class="footer">
    <div class="footer-hd">
        <p>
            <b>|</b> <a href="./home.html">商城首页</a> <b>|</b> <a
        >支付宝</a> <b>|</b> <a href="#">物流</a>
        </p>
    </div>
    <div class="footer-bd">
        <p style="text-align: center;">
            <a>合作伙伴</a> <a>联系我们</a> <a>网站地图</a> <em
                style="text-align: center;">© 2015-2025 zking 版权所有</em>
        </p>
    </div>
</div>
</div>
<div class="theme-popover-mask"></div>
<div class="theme-popover">
    <!--标题 -->
    <div class="am-cf am-padding">
        <div class="am-fl am-cf">
            <strong class="am-text-danger am-text-lg">新增地址</strong> / <small>Add
            address</small>
        </div>
    </div>
    <hr/>
    <div class="am-u-md-12">
        <form class="am-form am-form-horizontal">
            <div class="am-form-group">
                <label for="user-name" class="am-form-label">收货人</label>
                <div class="am-form-content">
                    <input type="text" id="user-name" placeholder="收货人">
                </div>
            </div>
            <div class="am-form-group">
                <label for="user-phone" class="am-form-label">手机号码</label>
                <div class="am-form-content">
                    <input id="user-phone" placeholder="手机号必填" type="email">
                </div>
            </div>
            <div class="am-form-group">
                <label for="user-phone" class="am-form-label">所在地</label>
                <div class="am-form-content address">

                    <div id="province"></div>
                </div>
            </div>
            <div class="am-form-group">
                <label for="user-intro" class="am-form-label">详细地址</label>
                <div class="am-form-content">
                    <textarea class="" rows="3" id="user-intro" placeholder="输入详细地址"></textarea>
                    <small>100字以内写出你的详细地址...</small>
                </div>
            </div>
            <div class="am-form-group theme-poptit">
                <div class="am-u-sm-9 am-u-sm-push-3">
                    <div class="am-btn am-btn-danger save">保存</div>
                    <div class="am-btn am-btn-danger close">取消</div>
                </div>
            </div>
        </form>
        <div class="error-div" style="color: red;margin-left: 20%">

        </div>
    </div>

    <script type="text/javascript" src="../js/jquery.provincesCity.js"></script>
    <script type="text/javascript" src="../js/provincesData.js"></script>
    <script type="text/javascript">
    </script>

</div>
<div class="clear"></div>
<script type="text/javascript">
    //判断用户是否已经登录
    function isLogin() {
        $.ajax({
            url: "http://localhost:8080/SnacksShop/user.do?type=isLogin",
            type: "post",
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                console.log(data);
                if (data == "101") {
                    location.href = "./login.html";
                    return;
                }
                data = JSON.parse(data); //解析为Json格式
                $("#user-login-label").hide(); //隐藏登录标签
                $("#userWellcome").html("欢迎登录&nbsp;&nbsp;<span style='color: #ff0000'>" + data.requestAPI.object.userName + "</span>");
                $("#unlogin-box-guanggao").hide();//隐藏
                $("#userImg-cebian").attr("src", data.requestAPI.object.userImg);//显示侧边用户头像
                $("#userName-cebian").text("用户名:" + data.requestAPI.object.userName);//显示侧边用户名
            },
            error: function () {

            }
        })
    }

    isLogin();
</script>
<script type="text/javascript">

    //读取url中的参数
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return decodeURIComponent(pair[1]);
            }
        }
        return false;
    }

    //关键字
    var tasteIDs = getQueryVariable("tasteIDs");

    if (!tasteIDs) {
        layer.msg('操作异常!', {icon: 1, time: 1500});
        location.href = "./shopcart.html";

    } else {
        getOrder();
        getAddress();
    }

    function getOrder() {
        //上传服务器
        $.ajax({
            url: "http://localhost:8080/SnacksShop/user.do?type=settlement",
            type: "post",
            crossDomain: true,
            data: {
                tasteIDs: tasteIDs
            },
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                data = JSON.parse(data);
                printOrder(data.data);
            },
            error: function () {
            }
        })
    }

    //获取 JSON 数组长度：
    function getJsonLength(json) {
        var jsonLength = 0;
        for (var i in json) {
            jsonLength++;
        }
        return jsonLength;
    }

    //获取唯一交易号

    function generateUUID() {
        var d = new Date().getTime();
        if (window.performance && typeof window.performance.now === "function") {
            d += performance.now(); //use high-precision timer if available
        }
        var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
    }


    function getAddress() {
        //获取 用户地址
        $.ajax({
            url: "http://localhost:8080/SnacksShop/user.do?type=getAddress",
            type: "post",
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                console.log(data);
                data = JSON.parse(data);
                printAddress(data.data);
            },
            error: function () {
            }
        })

    }

    var data_length

    //打印地址信息
    function printAddress(data) {
        //获取地址长度
        data_length = getJsonLength(data);
        var html = '';
        //查找默认地址的位置
        var temp = 0;
        for (var i = 0; i < data_length; i++) {
            if (data[i].isDefault == 1)
                temp = i;
        }
        html += '<div class="per-border"></div><li count="' + temp + '" class="user-addresslist defaultAddr" address="' + data[temp].addressId + '"><div class="address-left"><div class="user DefaultAddr">';
        html += '<span class="buy-address-detail"> <span class="buy-user">' + data[temp].addressUsername + '</span> <span class="buy-phone">' + data[temp].addressPhone + '</span>';
        html += '</span></div><div class="default-address DefaultAddr"><span class="buy-line-title buy-line-title-type">收货地址：</span> <span class="buy--address-detail"> ';
        html += '<span class="province">' + data[temp].addressContent + '</span><span class="city">' + data[temp].addressContent1 + '</span> &nbsp;<span class="dist">' + data[temp].addressContent2 + '</span><span class="street">' + data[temp].addressAddress + '</span>';
        html += '</span> </span></div><ins class="deftip">默认地址</ins></div><div class="address-right">';
        html += '<a href="../person/address.html"> <span class="am-icon-angle-right am-icon-lg"></span></a></div><div class="clear"></div><div class="new-addr-btn"><a class="hidden">设为默认</a> <span'
        html += ' class="new-addr-bar hidden">|</span> <a>编辑</a> <span class="new-addr-bar">|</span> <a class="delete-address" isDefault="true" addressID="' + data[temp].addressId + '">删除</a></div></li>';
        for (var i = 0; i < data_length; i++) {
            if (i != temp) {
                html += '<div class="per-border"></div><li count="' + i + '" class="user-addresslist " address="' + data[i].addressId + '"><div class="address-left">';
                html += '<div class="user DefaultAddr"><span class="buy-address-detail"> <span class="buy-user">' + data[i].addressUsername + '</span> <span class="buy-phone">' + data[i].addressPhone + '</span>'
                html += '</span></div><div class="default-address DefaultAddr"><span class="buy-line-title buy-line-title-type">收货地址：</span> <span class="buy--address-detail"> ';
                html += '<span class="province">' + data[i].addressContent + '</span><span class="city">' + data[i].addressContent1 + '</span>&nbsp; <span class="dist">' + data[i].addressContent2 + '</span><span class="street">' + data[i].addressAddress + '</span></span> </span>';
                html += ' </div><ins class="deftip hidden">默认地址</ins></div><div class="address-right"><span class="am-icon-angle-right am-icon-lg"></span></div><div class="clear"></div>';
                html += '<div class="new-addr-btn"><a>设为默认</a> <span class="new-addr-bar">|</span> <a>编辑</a> <span class="new-addr-bar">|</span> <a class="delete-address" isDefault="false" addressID="' + data[i].addressId + '">删除</a></div></li>';
            }
        }
        //修改末尾地址信息
        $(".address-pro").text(data[temp].addressContent);
        $(".address-city").text(data[temp].addressContent1);
        $(".address-dist").text(data[temp].addressContent2);
        $(".address-street").text(data[temp].addressAddress);
        $(".address-name").text(data[temp].addressUsername);
        $(".address-phone").text(data[temp].addressPhone);
        $(".pay-address").attr("values", data[temp].addressId)
        $(".address-ul").html(html);

        $(".user-addresslist").click(function () {
            $(".user-addresslist").removeClass("defaultAddr");
            $(this).addClass("defaultAddr");
            //修改末尾地址信息
            var i = $(this).attr("count");
            $(".address-pro").text(data[i].addressContent);
            $(".address-city").text(data[i].addressContent1);
            $(".address-dist").text(data[i].addressContent2);
            $(".address-street").text(data[i].addressAddress);
            $(".address-name").text(data[i].addressUsername);
            $(".address-phone").text(data[i].addressPhone);
            $(".pay-address").attr("values", data[i].addressId)
        })

        $(".delete-address").click(function () {

            if ($(this).attr("isDefault") == "true") {
                layer.msg('无法删除默认地址!', {icon: 1, time: 1500});
            }
            var addressID = $(this).attr("addressID");
            console.log(addressID)
            var temp2 = $(this);
            layer.confirm('确认删除该地址？', {icon: 0,}, function () {
                //获取地址ID
                //删除地址
                $.ajax({
                    url: "http://localhost:8080/SnacksShop/user.do?type=deleteAddress",
                    type: "post",
                    data: {
                        addressID: addressID
                    },
                    crossDomain: true,
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (datas) {
                        if (datas == "true") {
                            layer.msg('删除成功!', {icon: 1, time: 1500});
                            $(temp2).parents(".user-addresslist").remove();
                            $(".address-pro").text(data[temp].addressContent);
                            $(".address-city").text(data[temp].addressContent1);
                            $(".address-dist").text(data[temp].addressContent2);
                            $(".address-street").text(data[temp].addressAddress);
                            $(".address-name").text(data[temp].addressUsername);
                            $(".address-phone").text(data[temp].addressPhone);
                        } else
                            layer.msg('操作异常!', {icon: 1, time: 1500});
                    },
                    error: function () {
                    }
                })

            });
        })
    }

    //记录订单数目
    var temp_order = 1;

    //打印相关信息
    function printOrder(data) {
        var data_length = getJsonLength(data);
        temp_order = data_length;
        var html = '';
        var pay = 0;
        for (var i = 0; i < data_length; i++) {
            //总价格
            var allPrice = 0;
            var businessName = data[i][0].goods.businessName.businessName;
            html += '<div><div style="background-color: #aaa5a5">' + businessName + '</div>';
            html += '<div class="concent"><div id="payTable"><div class="cart-table-th"><div class="wp"><div class="th th-item"><div class="td-inner">商品信息</div>';
            html += '</div><div class="th th-price"><div class="td-inner">单价</div></div><div class="th th-amount"><div class="td-inner">数量</div></div>';
            html += '<div class="th th-sum"><div class="td-inner">金额</div></div></div></div><div class="clear"></div>';
            var goods_length = getJsonLength(data[i]);
            for (var j = 0; j < goods_length; j++) {
                html += '<tr class="item-list"><div class="bundle  bundle-last"><div class="bundle-main"><ul class="item-content clearfix"><div class="pay-phone"><li class="td td-item"><div class="item-pic"><a  class="J_MakePoint">';
                html += '<img src="' + data[i][j].carTaste.tasteImg + '" class="itempic J_ItemImg"></a></div><div class="item-info"><div class="item-basic-info">';
                html += '<a  class="item-title J_MakePoint" data-point="tbcart.8.11">' + data[i][j].goods.goodsName + '</a></div></div></li><li class="td td-info">';
                html += '<div class="item-props"><span class="sku-line">口味：' + data[i][j].carTaste.tasteName + '</span></div></li><li class="td td-price"><div class="item-price price-promo-promo"><div class="price-content">';
                html += '<tr class="item-list"><div class="bundle  bundle-last">';
                html += '<div class="bundle-main"><ul class="item-content clearfix"><div class="pay-phone"><li class="td td-item"><div class="item-pic"><a  class="J_MakePoint"> ';
                var price = data[i][j].carTaste.tastePrice;
                if (data[i][j].goods.goodsIsActivity == 1) {
                    price = (price * data[i][j].goods.goodsDiscount).toFixed(2);
                }
                html += '<em class="J_Price price-now">' + price + '</em></div></div></li></div><li class="td td-amount"><div class="amount-wrapper "><div class="item-amount "><span class="phone-title">购买数量</span>';
                html += '<div class="sl">' + data[i][j].carNumber + '</div></div></div></li><li class="td td-sum"><div class="td-inner"><em tabindex="0" class="J_ItemSum number">' + (data[i][j].carNumber * price).toFixed(2) + '</em>'
                html += '</div></li></ul><div class="clear"></div>';
                allPrice += data[i][j].carNumber * price;
            }
            html += '<div class="pay-total"><div class="order-extra"><div class="order-user-info"><div id="holyshit257" class="memo"><label>买家留言：</label> <input id="message-' + businessName + '" type="text"';
            html += ' placeholder="选填,建议填写和卖家达成一致的说明" class="memo-input J_MakePoint c2c-text-default memo-close">';
            html += '<div class="msg hidden J-msg"><p class="error">最多输入500个字符</p></div></div></div></div>';
            html += '<div class="buy-agio"><li class="td td-coupon"><span class="coupon-title">优惠券</span><select data-am-selected><option value="a">';
            html += '<div class="c-price"><strong>￥8</strong></div><div class="c-limit">【消费满95元可用】</div></option><option value="b" selected><div class="c-price"><strong>￥3</strong>';
            html += '</div><div class="c-limit">【无使用门槛】</div></option></select></li><li class="td td-bonus"><span class="bonus-title">红包</span> <select data-am-selected>';
            html += '<option value="a"><div class="item-info">¥50.00<span>元</span></div><div class="item-remainderprice"><span>还剩</span>10.40<span>元</span></div></option><option value="b" selected>';
            html += '<div class="item-info">¥50.00<span>元</span></div><div class="item-remainderprice"><span>还剩</span>50.00<span>元</span></div></option>';
            html += '</select></li></div><div class="clear"></div></div>';
            html += '<div class="buy-point-discharge "> <p class="price g_price ">合计（含运费） <span>¥</span><em class="pay-sum">' + (allPrice).toFixed(2) + '</em></p> </div></div></div>';
            pay += allPrice;
        }
        //打印订单列表
        $(".order-div").html(html);
        //打印实付金额
        $("#J_ActualFee").html((pay).toFixed(2));

        //点击提交按钮
        $("#J_Go").click(function () {
            var once_number = generateUUID();
            var all_money = $("#J_ActualFee").text();
            console.log(once_number);
            console.log(all_money);
            //上传订单
            for (var i = 0; i < temp_order; i++) {
                var getDataLength = getJsonLength(data[i]);
                var business_name = data[i][0].goods.businessName.businessName;
                var oncePrice = '';
                var tempNumber = '';
                var teaste = '';
                for (var j = 0; j < getDataLength; j++) {
                    if (data[i][j].goods.goodsIsActivity == 1) {
                        oncePrice += (data[i][j].carTaste.tastePrice * data[i][j].goods.goodsDiscount).toFixed(2) + "_";
                    } else {
                        oncePrice += data[i][j].carTaste.tastePrice + "_";
                    }
                    tempNumber += data[i][j].carNumber + "_";
                    teaste += data[i][j].carTaste.tasteId + "_";
                }
                //上传订单
                $.ajax({
                    url: "http://localhost:8080/SnacksShop/user.do?type=addOrder",
                    type: "post",
                    crossDomain: true,
                    data: {
                        oncePrice: oncePrice, //订单价格
                        tempNumber: tempNumber, //数量
                        onceNumber: once_number, //唯一订单交易号
                        business_name: encodeURI(business_name), //店铺名称
                        message: encodeURI(document.getElementById("message-" + business_name).value), //交易留言
                        addressID: $(".pay-address").attr("values"),
                        tasteID: teaste
                    },
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (data) {

                    },
                    error: function () {
                    }
                })
            }
            layer.msg('订单提交成功即将进入支付页面!', {icon: 1, time: 2500});
            location.href = "./success.html?type=aliPay&total_amount=" + all_money + "&subject=" + once_number;
            // location.href = "http://localhost:8080/SnacksShop/user.do
        })


    }


    $(function () {
        $("#province").ProvinceCity();
    });

    //点击保存按钮
    $(".save").click(function () {
        if (data_length >= 6) {
            $(".close").click();
            layer.msg('当前可用地址已超限制，请先删除后添加!', {icon: 1, time: 2000});
            return;
        }
        if ($("#user-name").val().length == 0) {
            $(".error-div").text("未填写收货人");
            return;
        }
        if ($("#user-phone").val().length == 0) {
            $(".error-div").text("未填写联系电话");
            return;
        }
        if ($("#user-intro").val().length == 0) {
            $(".error-div").text("未填写详细地址");
            return;
        }
        if ($("#province").find("select").eq(0).val() == "请选择") {

            $(".error-div").text("未选择地址");
            return;
        }
        if (!(/^1[34578]\d{9}$/.test($("#user-phone").val()))) {
            $(".error-div").text("手机号不符合规范");
            return;
        }
        var name = $("#user-name").val();
        $.ajax({
            url: "http://localhost:8080/SnacksShop/user.do?type=addAddress",
            type: "post",
            data: {
                name: encodeURI(name),
                phone: $("#user-phone").val(),
                address1: encodeURI($("#province").find("select").eq(0).val()),
                address2: encodeURI($("#province").find("select").eq(1).val()),
                address3: encodeURI($("#province").find("select").eq(2).val()),
                content: encodeURI($("#user-intro").val())
            },
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data == "true") {
                    $(".close").click();
                    layer.msg('添加成功!', {icon: 1, time: 1500});
                    getAddress();
                }
            },
            error: function () {
            }
        })
    })
    $("#user-name").change(function () {
        $(".error-div").text("");
    })
    $("#user-phone").change(function () {
        $(".error-div").text("");
    })
    $("#user-intro").change(function () {
        $(".error-div").text("");
    })
    $("#province").change(function () {
        $(".error-div").text("");
    })

</script>

</body>

</html>